package com.iuweb.user.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.iuweb.commen.domain.MenuResult;
import com.iuweb.commen.domain.Resource;
import com.iuweb.commen.domain.ShareUserResources;
import com.iuweb.login.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ruiwu
 * @version V1.0
 * @Description:用户Controller层
 */
@Controller
@RequestMapping("/common/user")
public class UserController {
    private final static Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private LoginService loginService;
    /**
     * 初步定为admin及普通用户
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ModelAndView main(HttpServletRequest request, Map<String, Object> map) {

        return new ModelAndView("wrapper.personal.statistics.showAllUser");
    }


    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getMenu", method = RequestMethod.GET)
    @ResponseBody
    public MenuResult getMenu(HttpServletRequest request) {
        List<Resource> resources = new ArrayList<Resource>();
        List<ShareUserResources> resourceList = loginService.getLoginResult("ruiwu", "123");
        List<Resource> children = new ArrayList<Resource>();
        List<Resource> parents = new ArrayList<Resource>();
        if (resourceList != null) {
            for (ShareUserResources shareUserResource : resourceList) {
                Resource resource = new Resource();
                resource.setName(shareUserResource.getResourceName());
                resource.setAccessUrl(shareUserResource.getResourceUrl());
                resource.setPid(shareUserResource.getParentRefid());
                resource.setCode(shareUserResource.getRefid());
                resource.setIconClass(shareUserResource.getIconClass());
                resource.setUniqueCode(shareUserResource.getUniqueCode());
                String type = shareUserResource.getResourceType();
                if (type != null && "file".equals(type)) {
                    parents.add(resource);
                } else {
                    children.add(resource);
                }
            }
            for (Resource resourceParent : parents) {
                String patrentCode = resourceParent.getCode();
                for (Resource resourceChild : children) {
                    String pCode = resourceChild.getPid();
                    if (pCode.equals(patrentCode)) {
                        List<Resource> pchildren = resourceParent.getChildren();
                        if (pchildren == null) {
                            pchildren = new ArrayList<Resource>();
                        }
                        pchildren.add(resourceChild);
                        resourceParent.setChildren(pchildren);
                    }
                }
            }
            resources = parents;
        }
        System.out.println(resources.toString());
        MenuResult result = new MenuResult();
        result.setResources(resources);
        return result;
    }
    /**
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "showAllUser",method= RequestMethod.GET)
    public ModelAndView showAllUser(Model model){
        return new ModelAndView("page.jsp.user.showAllUser");
    }

    @RequestMapping(value = "TestTelphone",method= RequestMethod.POST)
    public void testTelphone(@RequestParam("telphone") String telphone ,@RequestParam("telvalue") String telvalue) throws ClientException {
        //产品名称:云通信短信API产品,开发者无需替换
        String product = "Dysmsapi";
        //产品域名,开发者无需替换
        String domain = "dysmsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
        String accessKeyId = "LTAIhpwl1EsCbtji";
        String accessKeySecret = "wQ4yKqDvbBgHtR7Mj3v6S0Fel0W9HF";

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers("17671465596");
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("吴瑞");
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("SMS_122294089");
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{\"code\":\"123\"}");

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + sendSmsResponse.getCode());
        System.out.println("Message=" + sendSmsResponse.getMessage());
        System.out.println("RequestId=" + sendSmsResponse.getRequestId());
        System.out.println("BizId=" + sendSmsResponse.getBizId());

        log.info("介绍"+telphone+telphone);
    }
}
